import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ThemeColors {
  final Color primary;
  final Color secondary;
  final LinearGradient gradient;

  ThemeColors({required this.primary, required this.secondary})
    : gradient = LinearGradient(
        colors: [primary, secondary],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      );
}

class ThemeService {
  static const String _themeKey = 'selected_theme';

  static final Map<String, ThemeColors> themeColors = {
    '薰衣草': ThemeColors(
      primary: Color(0xFF9C27B0),
      secondary: Color(0xFF7B1FA2),
    ),
    '天空蓝': ThemeColors(
      primary: Color(0xFF2196F3),
      secondary: Color(0xFF1976D2),
    ),
    '粉红糖': ThemeColors(
      primary: Color(0xFFE91E63),
      secondary: Color(0xFFC2185B),
    ),
    '薄荷绿': ThemeColors(
      primary: Color(0xFF009688),
      secondary: Color(0xFF00796B),
    ),
    '暖阳橙': ThemeColors(
      primary: Color(0xFFFF9800),
      secondary: Color(0xFFF57C00),
    ),
  };

  static final Map<String, ThemeData> themes = Map.fromEntries(
    themeColors.entries.map((entry) {
      final colors = entry.value;
      return MapEntry(entry.key, _buildThemeData(colors));
    }),
  );

  static ThemeData _buildThemeData(ThemeColors colors) {
    return ThemeData(
      useMaterial3: true,
      scaffoldBackgroundColor: const Color(
        0xFFF0F0F3,
      ), // Light gray for neumorphic effect
      colorScheme: ColorScheme.fromSeed(
        seedColor: colors.primary,
        brightness: Brightness.light,
        surface: const Color(0xFFF0F0F3),
        background: const Color(0xFFF0F0F3),
      ),
      appBarTheme: AppBarTheme(
        elevation: 0,
        scrolledUnderElevation: 0,
        backgroundColor: colors.primary,
        surfaceTintColor: Colors.transparent,
        titleTextStyle: const TextStyle(
          color: Colors.white,
          fontSize: 20,
          fontWeight: FontWeight.bold,
        ),
        iconTheme: const IconThemeData(color: Colors.white),
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: colors.primary,
          foregroundColor: Colors.white,
        ),
      ),
      floatingActionButtonTheme: FloatingActionButtonThemeData(
        backgroundColor: colors.primary,
        foregroundColor: Colors.white,
      ),
      cardTheme: CardTheme(
        elevation: 2,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      ),
      chipTheme: ChipThemeData(
        backgroundColor: colors.primary.withOpacity(0.1),
        labelStyle: TextStyle(color: colors.primary),
      ),
      inputDecorationTheme: InputDecorationTheme(
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(8),
          borderSide: BorderSide(color: colors.primary, width: 2),
        ),
      ),
    );
  }

  static Future<String> getSelectedTheme() async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getString(_themeKey) ?? themes.keys.first;
  }

  static Future<void> setTheme(String themeName) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_themeKey, themeName);
  }
}
